package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ofps.forms.common.utils.R;
import com.ofps.forms.mapper.QuestionScoreMapper;
import com.ofps.forms.domain.QuestionScore;
import com.ofps.forms.service.QuestionScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionScoreService")
public class QuestionScoreServiceImpl extends ServiceImpl<QuestionScoreMapper, QuestionScore> implements QuestionScoreService {

    @Resource
    private QuestionScoreMapper questionScoreMapper;

    @Override
    public R save(HttpServletRequest request, QuestionScore questionScore) {
//        questionScore.setUserId(TokenUtil.getId(request));
        questionScore.setUserId(Math.toIntExact(getUserId()));
        return this.save(questionScore) ? R.ok("创建评分题成功！") : R.error("创建失败！");
    }

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return questionScoreMapper.delete(queryWrapper) == 1 ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public List<QuestionScore> queryQuestionScore(Map<String, Object> body) {
        QueryWrapper<QuestionScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionScoreMapper.selectList(queryWrapper);
    }
}
